package com.wl.cloud.system.security;

import cn.hutool.core.util.ObjUtil;
import com.wl.cloud.security.model.LoginUserDetails;
import com.wl.cloud.security.service.DynamicSecurityService;
import com.wl.cloud.security.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author: wanglin
 * @date: 2023-09-12 周二
 * @Version: 1.0
 * @Description:
 */
@Configuration
public class SystemSecurityConfig {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Bean
    public UserDetailsService userDetailsService() {
        //获取登录用户信息
        return username -> {
            LoginUserDetails loginUser = jwtTokenUtil.getLoginUser(username);
            if (ObjUtil.isNull(loginUser)) {
                throw new UsernameNotFoundException("用户名或密码错误");
            }
            return loginUser;
        };
    }

    @Bean
    public DynamicSecurityService dynamicSecurityService() {
        return new DynamicSecurityServiceImpl();
    }
}
